<?php
namespace Admin\Controller;
use Think\Controller;
class ActiveController extends Controller {
    // 修改摇一摇活动状态
    public function changesharkflag(){

        if(!check_admin_login()){
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'登录超时'));
            return ;
        }
        $flag=$_POST['flag'];

        $active=M('active');


        if($flag==1){
            $add_data=array(
                'activity_type'=>1,
                'activity_flag'=>$flag
            );

            $active_add_res=$active->data($add_data)->add();
            
            if($active_add_res){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'更新成功','return_body'=>array('activity_flag'=>$flag,'id'=>$active_add_res)));
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'更新失败'));
            }
        }else{
            $update_data=array(
                'activity_type'=>1,
                'activity_flag'=>0
            );
            $update_data_res=$active->where(array('activity_type'=>1))->save($update_data);
            if($update_data_res){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'更新成功','return_body'=>array('activity_flag'=>$flag)));
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'更新失败'));
            }

        }

    }

    // 查看摇一摇活动状态
    public function getsharkflag(){
        if(!check_admin_login()){
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'登录超时'));
            return ;
        }

        $active=M('active');
        
        $select_data=array(
            'activity_type'=>1,
        );

        $active_select_res=$active->where($select_data)->order('id desc')->select();

        if($active_select_res){
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'登录成功','return_body'=>$active_select_res[0]));
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有获取到数据'));
        }

    }

    
    // 获取摇一摇列表
    public function getusersharklist(){
        $active_id=$_POST['activeId'];
        $active_id=1;
        $handle_flag=$_POST['isRecord'];
        $limit_num=$_POST['limit'];

        if(!$limit_num){
            $limit_num=12;
        }

        if($active_id){
            $shark_num=D('SharkNum');

            $shark_num_select_res=$shark_num->relation(true)->where(array('active_id'=>$active_id))->limit($limit_num)->order('shark_num desc')->select();

            // 如果需要记录数据
            if($handle_flag==1){
                $winning_record=M('winning_record');
                
                foreach($shark_num_select_res as $key=>$row){
                    if($key<=2){
                        $add_data=array(
                            'winning_type'=>2,
                            'user_id'=>$row['user_id'],
                            'user_openid'=>$row['user_openid'],
                            'active_id'=>$active_id,
                            'winning_rank'=>$key+1
                        );    
                        $winning_record_add_res=$winning_record->data($add_data)->add();
                    }
                }
                
                // if($shark_num_select_res[0]){
                //     $add_data=array(
                //         'winning_type'=>2,
                //         'user_id'=>$shark_num_select_res[0]['user_id'],
                //         'user_openid'=>$shark_num_select_res[0]['user_openid'],
                //         'active_id'=>$active_id,
                //         'winning_rank'=>1
                //     );    
                //     $winning_record_add_res=$winning_record->data($add_data)->add();
                // }
    
                // if($shark_num_select_res[1]){
                //     $add_data1=array(
                //         'winning_type'=>2,
                //         'user_id'=>$shark_num_select_res[0]['user_id'],
                //         'user_openid'=>$shark_num_select_res[0]['user_openid'],
                //         'active_id'=>$active_id,
                //         'winning_rank'=>2
                //     );    
                //     $winning_record_add_res=$winning_record->data($add_data1)->add();
    
                // }
    
                // if($shark_num_select_res[2]){
                //     $add_data2=array(
                //         'winning_type'=>2,
                //         'user_id'=>$shark_num_select_res[0]['user_id'],
                //         'user_openid'=>$shark_num_select_res[0]['user_openid'],
                //         'active_id'=>$active_id,
                //         'winning_rank'=>3
                //     );    
                //     $winning_record_add_res=$winning_record->data($add_data2)->add();
                // }

            }

            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$shark_num_select_res));

        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }
    }


    // 保存用户中奖状态
    public function savedrawuser(){
        if(!check_admin_login()){
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'登录超时'));
            return ;
        }

        $openid=$_POST['openid'];
        $draw_name=$_POST['drawName'];
        if($openid && $draw_name){
            $user=M('user');
            $user_select_res=$user->where(array('user_openid'=>$openid))->select();
            if($user_select_res){
                $user_id=$user_select_res[0]['id'];        
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有该用户'));
                return ;        
            }



            $active=M('active');
            $active_add_data=array(
                'activity_type'=>2,
                'activity_flag'=>0
            );
            $active_add_res=$active->data($active_add_data)->add();

            $draw_user=M('draw_user');            
            if($active_add_res){
                $draw_user_add_data=array(
                    'user_id'=>$user_id,
                    'user_openid'=>$openid,
                    'active_id'=>$active_add_res,
                    'user_draw'=>$draw_name
                );

                $draw_user_add_res=$draw_user->data($draw_user_add_data)->add();

                // 添加记录
                $winning_record=M('winning_record');
                $add_data2=array(
                    'winning_type'=>1,
                    'user_id'=>$user_id,
                    'user_openid'=>$openid,
                    'active_id'=>$active_add_res,
                    'winning_rank'=>1
                );    
                $winning_record_add_res=$winning_record->data($add_data2)->add();



                if($draw_user_add_res){
                    $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'添加成功','return_body'=>$draw_user_add_data));                
                }else{
                    $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'添加失败'));            
                }


            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'添加失败'));    
            }

        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }
    
    }

    // 获取用户列表
    public function getuserlist(){
        $user=M('user');
        
        $user_select_res=$user->select();

        if($user_select_res){
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$user_select_res));
        }else{
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'还没人签到','return_body'=>array()));
        }
    }

    // 获取抽奖用户
    public function getcustomerlist(){
        $user=M('user');
        
        $user_select_res=$user->select();

        if($user_select_res){
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$user_select_res));
        }else{
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'还没人签到','return_body'=>array()));
        }
        
    }

    // 获取最美瞬间
    public function getuserimg(){
        $uploadimg=D('Uploadimg');
        $uploadimg_select_res=$uploadimg->relation(true)->select();
        if($uploadimg_select_res){
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$uploadimg_select_res));
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有图片'));
        }
    }


    // 获取预签到列表
    public function getPreUserList(){
        $user=M('presign');
        
        $user_select_res=$user->select();

        if($user_select_res){
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$user_select_res));
        }else{
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'还没人签到','return_body'=>array()));
        }
    }



    // 获取中奖用户列表
    public function getWiningRecordList(){
        // $winning_record=D('WinningRecord');
        // $winning_record_select_res=$winning_record->relation(true)->order('user_id')->select();
        
        $winning_record=D('WinningRecord');
        
        $winning_record_select_res=$winning_record
        // ->join('cj_shark_num ON cj_winning_record.user_id = cj_shark_num.user_id AND cj_winning_record.active_id = cj_shark_num.active_id ORDER BY cj_winning_record.user_id')
        ->relation(true)
        ->select();
        
        if($winning_record_select_res){
            foreach($winning_record_select_res as $k=>$v){
                if($v['winning_type']==2){
                    $shark_res=M('shark_num')->where(array('user_id'=>$v['user_id'],'active_id'=>$v['active_id']))->find();
                    $winning_record_select_res[$k]['shark_num']=$shark_res['shark_num'];
                }
            }
            $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$winning_record_select_res));
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有数据'));
        }

    }
}